using System;
using System.Collections.Generic;
using System.Text;

namespace GarbageTruck.TimeRange
{
    public class TimeRange : ITemporalExpression
    {

        #region CONSTRUCTORS

        public TimeRange()
        {
        }

        public TimeRange(string expresion)
        {
            string[] parameters = expresion.Split(',');
            this.FirstTime = TimeSpan.Parse(parameters[0]);
            this.SecondTime = TimeSpan.Parse(parameters[1]);
        }

        public TimeRange(TimeSpan firstTime, TimeSpan secondTime)
        {
            this.FirstTime = firstTime;
            this.SecondTime = secondTime;
        }

        #endregion CONSTRUCTORS

        #region FIELDS

        private TimeSpan m_FirstTime;
        private TimeSpan m_SecondTime;

        #endregion FIELDS

        #region PROPERTIES

        public TimeSpan FirstTime
        {
            get { return m_FirstTime; }
            set { m_FirstTime = value; }
        }

        public TimeSpan SecondTime
        {
            get { return m_SecondTime; }
            set { m_SecondTime = value; }
        }

        #endregion PROPERTIES

        #region METHODS

        public bool Contains(DateTime dateTime)
        {
            return (dateTime.TimeOfDay >= FirstTime && dateTime.TimeOfDay <= SecondTime);
        }

        public DateTime? GetNextOcurrence(DateTime dateTime)
        {
            if (dateTime.TimeOfDay < FirstTime)
            {
                return dateTime.Date.AddTicks(FirstTime.Ticks);
            }
            else if (dateTime.TimeOfDay >= FirstTime && dateTime.AddTicks(1).TimeOfDay <= SecondTime) return dateTime.AddTicks(1);
            else return null;
        }

        public DateTime? GetEventEnd(DateTime dateTime)
        {
            return dateTime;
        }

        public override string ToString()
        {
            return string.Format("TRNG({0},{1})",
                this.FirstTime.ToString(), 
                this.SecondTime.ToString());
        }

        #endregion METHODS

    }
}
